package com.ajk.server.bkcontroller;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ajk.server.controller.access.ArgIndexForPrintLog;
import com.ajk.server.model.HospitalDict;
import com.ajk.server.model.query.DoctorQuery;
import com.ajk.server.model.request.DoctorReq;
import com.ajk.server.model.response.DoctorRes;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.service.DoctorService;
import com.ajk.server.service.HospitalDictService;
import com.ajk.server.service.RegionDictService;
import com.ajk.server.utils.IntegerUtil;

@RequestMapping("/admin/doctor")
@Controller
public class AdminDoctorController {
	
	@Autowired
	RegionDictService regionDictService;
	
	@Autowired
	HospitalDictService hospitalDictService;
	
	@Autowired
	DoctorService doctorService;
	
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public ResultList<DoctorRes> findMyEasemodAccount(DoctorQuery query){
		ResultList<DoctorRes> result = new ResultList<>();
		
		Integer regionId = query.getRegionId();
		
		boolean deleteHospitalIds = false;
		if(CollectionUtils.isEmpty(query.getHospitalIds()) && IntegerUtil.isPositiveInteger(regionId)){
			List<Integer> regionids = regionDictService.findSelfAllChildrenId(regionId);
			if (CollectionUtils.isNotEmpty(regionids)) {
				deleteHospitalIds = true;
				
				List<HospitalDict> hospitalDicts = hospitalDictService.findByRegionIdsInCache(regionids);
				List<Integer> hids = new ArrayList<Integer>();
				if (CollectionUtils.isNotEmpty(hospitalDicts)) {
					for (HospitalDict hospitalDict : hospitalDicts) {
						hids.add(hospitalDict.getId());
					}
					query.setHospitalIds(hids);
				}
				else{
					hids.add(-1);
					query.setHospitalIds(hids);
				}
			}
		}
		
		List<DoctorRes> doctorRes =  doctorService.findDoctorByQuery(query);
		
		Integer count = doctorService.countDoctorByQuery(query);
		
		query.setCount(count);
		
		if(deleteHospitalIds){
			query.setHospitalIds(null);
		}
		result.setExt(query);
		
		result.setItemList(doctorRes);
		
		return result;
	}
	
	
	/**
	 * 添加专家
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = "multipart/form-data")
	@ResponseBody
	public ResultObject<Integer> add(DoctorReq doctor, Principal principal) {
		Integer id = doctorService.insertDoctor(doctor);
		return new ResultObject<>(id);
	}
	
	
	/**
	 * 修改专家
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/modify", method = RequestMethod.POST, consumes = "multipart/form-data")
	@ResponseBody
	public ResultObject<Integer> modify(DoctorReq doctor, Principal principal) {
		Integer id = doctorService.updateDoctor(doctor);
		return new ResultObject<>(id);
	}
	
}
